//
//  WBVisitorLoginView.swift
//  Weibo
//
//  Created by 任贤斌 on 2016/11/22.
//  Copyright © 2016年 Renkey. All rights reserved.
//

import UIKit
import SnapKit

//swift中使用代理
@objc protocol WBVisitorLoginViewDelegate: NSObjectProtocol {
    //在协议中定义方法的声明
    //用户将要登陆
    @objc optional func userWillLogin()
    //用户将要注册
    @objc optional func userWillRegister()
}

class WBVisitorLoginView: UIView {
    
    //声明代理属性 需要使用weak关键字来修饰
    weak var delegate: WBVisitorLoginViewDelegate?
    

    //MARK:外界设置界面信息的方法
    func updateInfo(title: String, imageName: String, isHome: Bool = false) {
        //修改文字
        self.tipLabel.text = title
        //修改图片
        self.circleView.image = UIImage(named: imageName)
        
        if isHome  {
            //是首页,执行动画
            startAnimation()
        } else {
            //不是首页
            self.largeHouse.isHidden = true
            //隐藏遮罩
            self.backView.isHidden = true
        }
    }
    
    //动画效果
    private func startAnimation() {
        let animator = CABasicAnimation(keyPath: "transform.rotation")
        //设置属性
        animator.repeatCount = MAXFLOAT
        animator.toValue = M_PI * 2
        animator.duration = 20
        //当动画效果完成后就从图层上移除,false就不会移除图层,默认是true
        animator.isRemovedOnCompletion = false
        //将动画添加到circleView的图层上
        circleView.layer.add(animator, forKey: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func loginBtnDidClick() {
        //调用代理方法
        delegate?.userWillLogin?()
    }
    @objc private func registerBtnDidClick() {
        delegate?.userWillRegister?()
    }
    
    //MARK: 设置UI界面
    private func setupUI() {
        
        //监听按钮的点击事件
        loginBtn.addTarget(self, action: #selector(loginBtnDidClick), for: .touchUpInside)
        registerBtn.addTarget(self, action: #selector(registerBtnDidClick), for: .touchUpInside)
        
        addSubview(circleView)
        addSubview(backView)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        //设置圈圈约束
        circleView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.snp.centerX)
            make.centerY.equalTo(self.snp.centerY).offset(-80)
        }
        //大房子约束
        largeHouse.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        //文案约束
        tipLabel.snp.makeConstraints { (make) in
            make.top.equalTo(circleView.snp.bottom).offset(20)
            make.centerX.equalTo(circleView)
            //make.width.equalTo(220)
        }
        //登陆约束
        loginBtn.snp.makeConstraints { (make) in
            make.left.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
        //注册约束
        registerBtn.snp.makeConstraints { (make) in
            make.right.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
        //约束遮罩
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        
        //设置背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
    }
    
    //实例化子控件
    //遮罩
    private lazy var backView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    //圈圈
    private lazy var circleView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    //大房子
    private lazy var largeHouse: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    //提示文案
    private lazy var tipLabel: UILabel = {
        let l = UILabel(title: "关注一些人,快回到这里看看有什么惊喜", fontSize: 14, textColor: UIColor.darkGray)
        //换行
        l.numberOfLines = 0
        //设置居中
        l.textAlignment = .center
        //文本计算宽度 推荐 不要约束左右边距
        l.preferredMaxLayoutWidth = 220
        return l
    }()
    //登陆按钮
    lazy var loginBtn: UIButton = {
        let btn = UIButton(title: "登陆", fontSize: 14, textColor: UIColor.darkGray)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    //注册按钮
    lazy var registerBtn: UIButton = {
        let btn = UIButton(title: "注册", fontSize: 14, textColor: UIColor.orange)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()

    

}
